//
//  OMGoodDetailViewController.m
//  OMENGO
//
//  Created by 梁尚嘉 on 2017/4/19.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMGoodDetailViewController.h"
#import "OMGoodDetailHeadView.h"
#import "OMGoodDetailBottomBar.h"
#import "OMGoodDetailRecommendTableCell.h"
#import "OMProduct.h"

@interface OMGoodDetailViewController () <OMGoodDetailRecommendCollectCellDelegate>
@property (nonatomic, strong) OMGoodDetailBottomBar *bottomBar;
@property (nonatomic, strong) OMProduct *product;
@property (nonatomic, strong) NSArray <OMProduct*>*recommends;
@property (nonatomic, weak) OMGoodDetailHeadView *headView;
@property (nonatomic, strong) UIBarButtonItem *clearBarItem;
@property (nonatomic, strong) UIBarButtonItem *blackBarItem;
@end

@implementation OMGoodDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setupUI];
    [self startAnimating:YES];
    [self getProductDetail];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self scrollViewDidScroll:self.tableView];
    SMNotificationPost(SM_NOTIFACATION_NEED_CARTITEM_UPDATE, nil);
}


- (void)reloadDataFromNetWork
{
    [self getProductDetail];
}

- (void)getProductDetail
{
    [self.task cancel];
    self.task =
    [KTHttpUtils GET:@"goods/detail.jhtml"
          parameters:@{@"id":self.productId}
           needCache:YES
           needToken:NO
            progress:nil
          completion:^(id responseObject, BOOL isSuccess, NSString *message) {
              if (isSuccess) {
                  BOOL version_new = responseObject[OMDatumKey][@"goods"];
                  if (version_new) {
                      self.product = [OMProduct mj_objectWithKeyValues:responseObject[OMDatumKey][@"goods"]];
                      self.recommends = [OMProduct mj_objectArrayWithKeyValuesArray:responseObject[OMDatumKey][@"recommends"]];
                      for (OMProduct *product in self.recommends) {
                          product.image = [responseObject[OMImagePrefix] stringByAppendingString:product.image?:@""];
                      }
                  }else {
                      self.product = [OMProduct mj_objectWithKeyValues:responseObject[OMDatumKey]];
                  }
                  self.product.image = [responseObject[OMImagePrefix] stringByAppendingString:self.product.image?:@""];

                  [self reload];
                  [self startAnimating:NO];
              }else {
                  [self showNetWorkError];
              }
    }];
}



- (void)reload
{
    [self.headView configureWithObj:self.product];
    [self.bottomBar setProduct:self.product];
    [self.tableView reloadData];
}

- (void)setupUI
{
    _clearBarItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"ic_navibar_back_round_clear"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];

    _blackBarItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"ic_navibar_back_round_black"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];

    self.title = @"商品详情";
    self.navigationBar.translucent = true;
    self.tableView.tableHeaderView = [OMGoodDetailHeadView new];
    _headView = (id)self.tableView.tableHeaderView;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];

    self.bottomBar = [OMGoodDetailBottomBar new];
    [self.view addSubview:self.bottomBar];
    [self.bottomBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@(SM_SIZE(120)));
    }];

    [self.tableView setContentInset:UIEdgeInsetsMake(0, 0, SM_SIZE(120), 0)];
}


#pragma mark - tableview delegate && datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMGoodDetailRecommendTableCell * cell = [OMGoodDetailRecommendTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:self.recommends];
    cell.delegate = self;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [OMGoodDetailRecommendTableCell cellHeightWithItems:self.recommends];
}


#pragma mark - scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offsetY = scrollView.contentOffset.y;
    CGFloat alpha = MIN(offsetY/SM_SIZE(600), 1) ;
    self.navigationColor = [SM_whiteColor colorWithAlphaComponent:alpha];
    self.navigationShadowHidden = alpha != 1;
    self.title = alpha != 1 ? @"":@"商品详情";
    if (alpha >= 1) {
        self.navigationItem.leftBarButtonItem == _clearBarItem ?:[self.navigationItem setLeftBarButtonItem:_clearBarItem];
    }else {
        self.navigationItem.leftBarButtonItem == _blackBarItem ?:[self.navigationItem setLeftBarButtonItem:_blackBarItem];
    }
    [self.headView.header layoutHeaderViewForScrollViewOffset:scrollView.contentOffset];
}

#pragma mark - OMGoodDetailRecommendCollectCellDelegate
- (void)didClickPlusButton:(UIButton *)button withProduct:(OMProduct *)product
{
    [OMFMDBCartHelper itemAddition:product
               withButtonAnimation:button
                        targetView:self.bottomBar.badgeView];
}
@end
